import time
from tkinter import filedialog, scrolledtext

from utils.mylogger import MyLogger
from views.tabs.tab_base import TabBase
from utils.router_management import upgrade_dut
from ttkbootstrap.constants import *

class LocalUpgradeTab(TabBase):
    def __init__(self, ttk, style, notebook, name):
        super().__init__(ttk, style, notebook, name)
        self.create_widgets()
        self.button2_status = True

    def create_widgets(self):
        # 创建标签页
        tab = self.ttk.Frame(self.notebook)
        tab.pack(fill=BOTH, expand=True)
        self.notebook.add(tab, text=self.name)

        # self.style.configure('a.TButton', font=('Arial', 10), background='#fff', foreground='#000', borderwidth=1, bordercolor='red', relief='solid')
        # button1.configure(style='a.TButton')

        # 创建旧版本固件选择容器框架
        frame_old_version = self.ttk.Frame(tab)
        frame_old_version.pack(side="top")
        # 选择按钮
        button_old_version = self.ttk.Button(frame_old_version, text="旧版本固件", width=10, command=lambda: self.upload_file(self.entry_old_version))
        button_old_version.pack(side='left', padx=6)
        # 输入框:文件名
        self.entry_old_version = self.ttk.Entry(frame_old_version)
        self.entry_old_version.pack(side='left', padx=6)

        # 创建新版本固件选择容器框架
        frame_new_version = self.ttk.Frame(tab)
        frame_new_version.pack(side="top", pady=6)
        # 选择按钮
        button_new_version = self.ttk.Button(frame_new_version, text="新版本固件", width=10, command=lambda: self.upload_file(self.entry_new_version))
        button_new_version.pack(side='left', padx=6)
        # 输入框:文件名
        self.entry_new_version = self.ttk.Entry(frame_new_version)
        self.entry_new_version.pack(side='left', padx=6)

        # 创建账号容器框架
        frame_user = self.ttk.Frame(tab)
        frame_user.pack(side="top", pady=6)
        # 输入框:管理员账号
        label_user = self.ttk.Button(frame_user, text="账号", width=10, state='disabled', style='success-link')
        self.style.configure('a.TButton', font=('Arial', 10), background='#fff', foreground='#fff')
        label_user.configure(style='a.TButton')
        label_user.pack(side='left', padx=6)
        self.entry_user = self.ttk.Entry(frame_user)
        self.entry_user.insert(self.ttk.END, "user")
        self.entry_user.pack(side='left', padx=6)

        # 创建密码容器框架
        frame_pwd = self.ttk.Frame(tab)
        frame_pwd.pack(side="top", pady=6)
        # 输入框:管理员密码
        label_pwd = self.ttk.Button(frame_pwd, text="密码", width=10, style='success-link')
        label_pwd.pack(side='left', padx=6)
        self.entry_pwd = self.ttk.Entry(frame_pwd)
        self.entry_pwd.insert(self.ttk.END, "88888888")
        self.entry_pwd.pack(side='left', padx=6)

        # 创建IP地址容器框架
        frame_ip_address = self.ttk.Frame(tab)
        frame_ip_address.pack(side="top", pady=6)
        # 输入框:升级文件服务器IP地址
        label_ip_address = self.ttk.Button(frame_ip_address, text="I P 地 址", width=10, style='success-link')
        label_ip_address.pack(side='left', padx=6)
        self.entry_ip_address = self.ttk.Entry(frame_ip_address)
        self.entry_ip_address.insert(self.ttk.END, "192.168.10.1")
        self.entry_ip_address.pack(side='left', padx=6)

        # 创建循环升级次数容器框架
        frame_times = self.ttk.Frame(tab)
        frame_times.pack(side="top", pady=6)
        # 输入框:设置循环次数
        label_times = self.ttk.Button(frame_times, text="循环次数", width=10, style='success-link')
        label_times.pack(side='left', padx=6)
        self.entry_times = self.ttk.Entry(frame_times)
        self.entry_times.insert(self.ttk.END, "10")
        self.entry_times.pack(side='left', padx=6)

        # 创建按钮状态容器
        button_status = self.ttk.Frame(tab)
        button_status.pack(side="top", pady=6)
        # 创建确认按钮
        self.confirm_button = self.ttk.Button(button_status, text="开始更新固件", command=lambda : self.thread_it(self.upgrade_version) )
        self.confirm_button.pack(side="left", padx=20)

        # 创建结果显示容器框架
        frame_result = self.ttk.Frame(tab)
        frame_result.pack(side="top", padx=6)
        # 创建滚动条
        scrollbar = self.ttk.Scrollbar(frame_result)
        # scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
        # 创建结果显示框
        entry_result = scrolledtext.ScrolledText(frame_result, height=15, width=50, yscrollcommand=scrollbar.set)
        entry_result.pack()
        # 配置滚动条与Text控件的关联
        scrollbar.config(command=entry_result.yview)
        self.entry_result = entry_result

    def upload_file(self, entry_name):
        """
        获取上传文件的绝对路径并输入到控件内
        @param entry_name: 输入控件名
        """
        file_path = filedialog.askopenfilename()
        entry_name.delete(0, self.ttk.END)  # 清空输入框
        entry_name.insert(self.ttk.END, file_path)  # 在输入框中插入文件路径

    def upgrade_version(self):
        # 禁用确认按钮
        self.confirm_button.config(text="固件正在更新", state="disabled")
        # 获取输入框中的内容
        loop = int(self.entry_times.get())
        ip_address = self.entry_ip_address.get()
        old_version = self.entry_old_version.get()
        new_version = self.entry_new_version.get()
        web_user = self.entry_user.get()
        web_pwd = self.entry_pwd.get()

        logger = MyLogger('local upgrade')
        upgrade_dut(old_version, new_version, ip_address, web_user, web_pwd, loop, logger, self.update_result)
        time.sleep(10)

        # 启用确认按钮
        self.confirm_button.config(text='开始更新固件', state="normal")

    def update_result(self, content):
        """
            添加修改记录
            修改记录:追加模式
        """
        content = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) +"："+ content
        # entry_result.delete("1.0", tk.END)
        self.entry_result.insert(self.ttk.END, content)
        self.entry_result.insert(self.ttk.END, '\n')
        self.entry_result.see(self.ttk.END)  # 滚动到最后一行
